import { createStore, applyMiddleware } from 'redux';
import { composeWithDevTools } from 'redux-devtools-extension';
import withRedux from 'next-redux-wrapper';
import nextReduxSaga from 'next-redux-saga';
import createSagaMiddleware from 'redux-saga';

// reducer sagas
import reducers from './reducerIndex';
import sagas from './sagaIndex';

const sagaMiddleware = createSagaMiddleware();

export function configureStore(initialState = {}) {
  const store = createStore(
    reducers,
    initialState,
    composeWithDevTools(applyMiddleware(sagaMiddleware)),
  );

  store.sagaTask = sagaMiddleware.run(sagas);
  return store;
}

export function withReduxSaga(BaseComponent, options) {
  return withRedux(configureStore, options)(nextReduxSaga(BaseComponent));
}
